package com.sunny.hp.common.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.sunny.hp.common.controller.service.PubService;
import com.sunny.hp.common.servlet.ModelAndNCOMZView;
import com.sunny.hp.common.util.HttpRequest;

/**
 * 세션에 있는 로케일을 변환 하는 Controller 클래스
 */


@Controller
@RequestMapping("/pub")
public class PubController {


	@Autowired
	private PubService pubService;
	/**  퍼블리싱 진행 관리 화면 					
	 * @Method Name  : selectMemberInfoListView   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 퍼블리싱 진행 관리 화면 	                       		
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/selectPubMgmtView.do")
	public ModelAndNCOMZView selectPubMgmtView(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request);
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind(); 
		
		LinkedHashMap<String, LinkedHashMap> functionMap =new LinkedHashMap<String, LinkedHashMap>();
		ArrayList<LinkedHashMap> functionList = new ArrayList<LinkedHashMap>();
		functionList = (ArrayList<LinkedHashMap>)pubService.selectPubFunctionList(param);
		
		for(LinkedHashMap map : functionList){
			functionMap.put(map.get("pf_requestmapping")+"", map);
		}
		mav.addObject("functionMap", functionMap); 
		mav.setViewName("pub/selectPubMgmtView");
		return mav;
	}
	
	/**  퍼블리싱 진행 상황 데이터 가져오기 					
	 * @Method Name  : selectMemberInfoList   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 퍼블리싱 진행 상황 데이터 가져오기	                       		
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/ajax/selectPubFunctionList.do")
	public ModelAndNCOMZView selectPubFunctionList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request,new MappingJacksonJsonView());
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind(); 

		ArrayList<LinkedHashMap> functionList = new ArrayList<LinkedHashMap>();
		functionList = (ArrayList<LinkedHashMap>)pubService.selectPubFunctionList(param);
		mav.addObject("functionList", functionList); 
		return mav;
	}
	
	/**  퍼블리싱 진행 상황 데이터 업데이트 하기 					
	 * @Method Name  : updatePubFunctionInfo   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 퍼블리싱 진행 상황 데이터 업데이트 하기 	                       		
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/ajax/updatePubFunctionInfo.do")
	public ModelAndNCOMZView updatePubFunctionInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request,new MappingJacksonJsonView());
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind(); 
		
		System.out.println("#param ===> " + param);

		//변경 이력 사항 확인
		LinkedHashMap<String, String[]> modifyHistoryMap = new LinkedHashMap<String, String[]>();
		/*
		 * 1. insert/update 를 통해 퍼블리싱 정보를 등록한다.
		 * 2. 변경 사항에 대한 이력 사항을 등록한다.
		 */
		ArrayList<LinkedHashMap> functionList = new ArrayList<LinkedHashMap>();
		functionList = (ArrayList<LinkedHashMap>)pubService.selectPubFunctionList(param);
		
		LinkedHashMap functionInfo = new LinkedHashMap();
		if(functionList.size() == 1){
			functionInfo = functionList.get(0);
			Iterator it = param.keySet().iterator();
			while(it.hasNext()){
				String key = (String)it.next();	
				if(key.indexOf("_arr") > -1){
					continue;
				}
				String value = param.get(key)+"";
				
				String org_value = "";
				if(functionInfo.containsKey(key)){
					org_value = functionInfo.get(key)+"";
				}
				
				
				if(!value.equals(org_value)){
					String [] modifyHistory = new String[2];
					modifyHistory[0] = org_value;
					modifyHistory[1] = value;
					modifyHistoryMap.put(key, modifyHistory);
				}
			}
			if(modifyHistoryMap.size() > 0){
				param.put("pub_schedule_date", (param.get("pub_schedule_date")+"").replaceAll("-", ""));
				param.put("pub_end_date", (param.get("pub_end_date")+"").replaceAll("-", ""));
				pubService.updatePubFunctionInfo(param);
			}
//			System.out.println(modifyHistoryMap);
		}
		
		return mav;
	}
}
